// store.js
import { createStore } from 'vuex';

export default createStore({
  state: {
    data: null, // 用于存储页面数据
    userInfo: null, // 用户信息
    isLogin: false, // 登录状态
    businessId: null, // 门店ID
    businessList: [] // 门店列表
  },
  getters: {
    // 获取数据
    getData: state => state.data,
    // 获取用户信息
    getUserInfo: state => state.userInfo,
    // 获取登录状态
    getIsLogin: state => state.isLogin,
    // 获取当前门店ID
    getBusinessId: state => state.businessId,
    // 获取门店列表
    getBusinessList: state => state.businessList
  },
  mutations: {
    // 设置数据
    setData(state, payload) {
      state.data = payload;
    },
    // 设置用户信息
    setUserInfo(state, payload) {
      state.userInfo = payload;
      state.isLogin = !!payload;
    },
    // 设置门店ID
    setBusinessId(state, payload) {
      state.businessId = payload;
    },
    // 设置门店列表
    setBusinessList(state, payload) {
      state.businessList = payload;
    },
    // 退出登录
    logout(state) {
      state.userInfo = null;
      state.isLogin = false;
      state.businessId = null;
    }
  },
  actions: {
    // 刷新数据
    refreshData({ commit }) {
      return new Promise((resolve) => {
        setTimeout(() => {
          commit('setData', new Date().toLocaleTimeString());
          resolve();
        }, 1000);
      });
    },
    // 登录操作
    login({ commit }, userInfo) {
      return new Promise((resolve) => {
        // 这里可以调用登录API
        commit('setUserInfo', userInfo);
        resolve(userInfo);
      });
    },
    // 获取门店列表
    getBusinessList({ commit }) {
      return new Promise((resolve) => {
        // 这里可以调用获取门店列表的API
        // 模拟数据
        const mockData = [
          { businessId: 1, businessName: '门店1' },
          { businessId: 2, businessName: '门店2' }
        ];
        commit('setBusinessList', mockData);
        // 设置默认门店
        if (mockData.length > 0) {
          commit('setBusinessId', mockData[0].businessId);
        }
        resolve(mockData);
      });
    }
  }
});